package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zyf.vc.ui.PlayVideoActiviy;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.componet.MyScrollGridView;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.user.ImageViewPagerActivity;

/**
 * Created by AAA on 2017/4/27.
 */

public class YxdAddEvaluationAdapter extends BaseAdapter{

    private Context context;
    private ArrayList<tempLostRR> list;
    private BtnOnClickLisener onClickLisener;

    public YxdAddEvaluationAdapter(Context context,ArrayList<tempLostRR> list,BtnOnClickLisener onClickLisener){
        this.context=context;
        this.list=list;
        this.onClickLisener=onClickLisener;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.yxd_add_evaluation,parent,false);
            holder=new ViewHolder();
            holder.EvaTitle= (TextView) convertView.findViewById(R.id.EvaTitle);
            holder.textContent= (TextView) convertView.findViewById(R.id.textContent);
            holder.modifiEva= (TextView) convertView.findViewById(R.id.modifiEva);
            holder.deleteEva= (TextView) convertView.findViewById(R.id.deleteEva);
            holder.pictureList= (MyScrollGridView) convertView.findViewById(R.id.pictureList);
            holder.videoList= (MyScrollGridView) convertView.findViewById(R.id.videoList);
            holder.taskScore= (TextView) convertView.findViewById(R.id.taskScore);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        final tempLostRR rr=list.get(position);
        if (rr.getJpgStr()!=null&&!TextUtils.isEmpty(rr.getJpgStr())){
            holder.pictureList.setVisibility(View.VISIBLE);
            List<String> pic=new ArrayList<>();
            String[] jpg=rr.getJpgStr().split(",");
            //由于是本地加载，判断用户是否删除文件，如果删除这移出此项
            for (int i=0;i<jpg.length;i++){
                // TODO: 2017/7/6  1.上传时处理没有图片的问题，2.此处处理,没有图片的删除
                File file =new File(jpg[i]);
                if (file.exists()){
                    pic.add(jpg[i]);
                }
            }
            if (pic!=null&&pic.size()!=0){
            }else {
                holder.pictureList.setVisibility(View.GONE);
            }
            ImgGradeAdapter adapter=new ImgGradeAdapter(context,pic);
            holder.pictureList.setAdapter(adapter);
            holder.pictureList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

              //      String path= (String) parent.getItemAtPosition(position);
                    context.startActivity(new Intent(context, ImageViewPagerActivity.class)
                            .putExtra("filepath",rr.getJpgStr()).putExtra("position",position));
                }
            });


        }else{
            holder.pictureList.setVisibility(View.GONE);
        }
        if (rr.getVideoStr()!=null&&!TextUtils.isEmpty(rr.getVideoStr())){
            holder.videoList.setVisibility(View.VISIBLE);
            String[] video=rr.getVideoStr().split(",");
            List<String> list=new ArrayList<>();
            for (int i=0;i<video.length;i++){
                File file=new File(video[i]);
                if (file.exists()){
                    list.add(video[i]);
                }
            }
            //判断是否为空，空着不显示
            if (list!=null&&list.size()!=0){
            }else{
                holder.videoList.setVisibility(View.GONE);
            }
            ImgGradeAdapter adapter=new ImgGradeAdapter(context,list);
            holder.videoList.setAdapter(adapter);
            holder.videoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String path= (String) parent.getItemAtPosition(position);
                    context.startActivity(new Intent(context,PlayVideoActiviy.class).putExtra("file_path",path).putExtra("code_play",2));
                    //视频播放，单个
                }
            });
        }else{
            holder.videoList.setVisibility(View.GONE);
        }
        holder.EvaTitle.setText(rr.getRowName());
        holder.taskScore.setText("评分："+rr.getScore());
        holder.taskScore.setTextColor(ContextCompat.getColor(context,R.color.delayTask));
        if(rr.getDescStr()==null|| TextUtils.isEmpty(rr.getDescStr())){
            holder.textContent.setVisibility(View.GONE);
        }else {
            holder.textContent.setText("描述："+rr.getDescStr());
        }
        holder.modifiEva.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tempLostRR rr=list.get(position);
                onClickLisener.modifiClick(rr);
            }
        });
        holder.deleteEva.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onClickLisener.deleteClick(position);
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView EvaTitle;
        MyScrollGridView pictureList;
        MyScrollGridView videoList;
        TextView textContent;
        TextView modifiEva;
        TextView deleteEva;
        TextView taskScore;
    }

    public interface BtnOnClickLisener{
        void modifiClick(tempLostRR bean);
        void deleteClick(int position);
    }


}
